// 守卫导航
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from './router'
import store from './store'

const whitelist = ['/login', '/404']

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  NProgress.start()
  // 有token 代表登录
  if (token) {
    if (to.path === '/login') {
      next('/dashboard')
      NProgress.done()
    } else {
      next()
    }
  } else {
    // 没有token但是在白名单中
    if (whitelist.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

// 进入相关页面执行
router.afterEach((to, from) => {
  NProgress.done()
})
